package Model;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;

import Dao.LocationManageDao;
import Entity.Location;

public class LocationManageModel extends AbstractTableModel{
	private static final long serialVersionUID = 1L;
	private List<Location> locationList = new Vector<Location>();
	LocationManageDao dao = new LocationManageDao();
	private String[] header = { "STT", "Location Name", "Location Allowance" };

	public LocationManageModel() throws IOException, ClassNotFoundException,
			SQLException {
		locationList = dao.getLocationList();
	}

	public int getColumnCount() {
		// TODO Auto-generated method stub
		return header.length;
	}

	public int getRowCount() {
		// TODO Auto-generated method stub
		return locationList.isEmpty() == true ? 0 : locationList.size();
	}

//	public boolean isCellEditable(int rowIndex, int columnIndex) {
//		// TODO Auto-generated method stub
//		return super.isCellEditable(rowIndex, columnIndex);
//	}
//
//	public Class<?> getColumnClass(int columnIndex) {
//		// TODO Auto-generated method stub
//		if (columnIndex == 0) {
//			return Integer.class;
//		}
//		return String.class;
//	}
	
	public String getColumnName(int column) {
		// TODO Auto-generated method stub
		return header[column];
	}

	public List<Location> getLocationList() {
		return locationList;
	}

	public void setLocationList(List<Location> locationList) {
		this.locationList = locationList;
		fireTableDataChanged();
	}


	public Object getValueAt(int rowIndex, int columnIndex) {
		// TODO Auto-generated method stub
		if (locationList.isEmpty()) {
			return null;
		}
		Location location = locationList.get(rowIndex);
		switch (columnIndex) {
		case 0:
			return rowIndex + 1;
		case 1:
			return location.getName();
		case 2:
			return location.getAllowance();
		}
		return null;
	}

	public void addLocation(Location location) throws IOException,
			ClassNotFoundException, SQLException {
		dao.insert(location);
		locationList.add(location);
		fireTableDataChanged();
	}

	public void editLocation(int row, Location location) throws IOException, ClassNotFoundException, SQLException {
		locationList.set(row, location);
		dao.update(location);
		fireTableDataChanged();
	}

	public void deleteLocation(int row, Location location) throws IOException, ClassNotFoundException, SQLException {
		locationList.remove(row);
		dao.delete(location);
		fireTableDataChanged();
	}
}
